package com.ysd.lis.controller.interfacePlatform;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.ysd.common.Result;
import com.ysd.lis.controller.pub.BaseController;
import com.ysd.lis.entity.*;
import com.ysd.lis.entity.lab.LabMaininfo;
import com.ysd.lis.mapper.lab.LabMaininfoMapper;
import com.ysd.lis.mapper.sys.SysInterfaceMapper;
import com.ysd.lis.service.SysInterfaceCaUserService;
import com.ysd.lis.service.SysInterfaceService;
import com.ysd.lis.service.SysUserService;
import com.ysd.util.RedisUserManager;
import io.swagger.annotations.Api;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 * 平台字典表
 * 前端控制器
 * </p>
 *
 * @author pengchong
 * @since 2024-07-15
 */
@Api(tags = "平台系统类型表")
@RestController
@RequestMapping("/api/sys/SysInterfaceCaUser")
public class SysInterfaceCaUserController extends BaseController {
    @Autowired
    SysInterfaceCaUserService sysInterfaceCaUserService;
    @Autowired
    SysUserService sysUserService;
    @Autowired
    SysInterfaceService sysInterfaceService;
    @Autowired
    LabMaininfoMapper labMaininfoMapper;
    @Autowired
    SysInterfaceMapper sysInterfaceMapper;

    @PostMapping("/addSysInterfaceCaUser")
    public Result addSysInterfaceFunction(@RequestBody SysInterfaceCaUser sysInterfaceCaUser) {
        LambdaQueryWrapper<SysInterfaceCaUser> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SysInterfaceCaUser::getUsername, sysInterfaceCaUser.getUsername()).eq(SysInterfaceCaUser::getOrgId, sysInterfaceCaUser.getOrgId());
        boolean save = sysInterfaceCaUserService.saveOrUpdate(sysInterfaceCaUser, lambdaQueryWrapper);
        if (save) {
            return Result.succ(1, "添加成功", sysInterfaceCaUser);
        }
        return Result.fail("添加失败");
    }

    @PostMapping("/editSysInterfaceCaUser")
    public Result editSysInterfaceFunction(@RequestBody SysInterfaceCaUser sysInterfaceCaUser) {
        boolean b = sysInterfaceCaUserService.updateById(sysInterfaceCaUser);
        if (b) {
            return Result.succ(1, "添加成功", "");
        }
        return Result.fail("添加失败");
    }


    @GetMapping("/findInterfaceCaUserList")
    public Result findInterfaceCaUserList(String interfaceId) {
        MPJLambdaWrapper<SysUser> mpjLambdaWrapper = new MPJLambdaWrapper<>();
        mpjLambdaWrapper.selectAll(SysUser.class)
                .selectAs("dict", SysInterfaceCaUser::getCaUserId, SysUser::getCaUserId)
                .selectAs("dict", SysInterfaceCaUser::getSignimg, SysUser::getSignimg)
                .selectAs("dict", SysInterfaceCaUser::getIdCard, SysUser::getIdCard)
                .selectAs("dict", SysInterfaceCaUser::getActiveCode, SysUser::getActiveCode)
                .selectAs("dict", SysInterfaceCaUser::getActiveQrCode, SysUser::getActiveQrCode)
                .leftJoin(SysInterfaceCaUser.class, "dict", p -> p.eq(SysInterfaceCaUser::getUsername, SysUser::getUsername).eq(SysInterfaceCaUser::getInterfaceId, interfaceId).eq(SysInterfaceCaUser::getDelFlag, 0))
                .eq(SysUser::getDelFlag, 0);
        List<SysUser> list = sysUserService.list(mpjLambdaWrapper);
        return Result.succ(1, "查询成功", list);
    }


    @GetMapping("/findIsChkMainInfoByPatNo")
    public Result findIsChkMainInfoByPatNo(LabMaininfoPageParam labMaininfoPageParam) {
        //查询出所有的人员进行比对
        MPJLambdaWrapper<LabMaininfo> mpjLambdaWrapper = new MPJLambdaWrapper<>();
        mpjLambdaWrapper.selectAll(LabMaininfo.class)
                .selectAs("test", SysUser::getRealname, LabMaininfo::getTestDoctorNa)
                .selectAs("chk", SysUser::getRealname, LabMaininfo::getChkDoctorNa)
                .leftJoin(SysUser.class, "test", SysUser::getUsername, LabMaininfo::getTestDoctor)
                .leftJoin(SysUser.class, "chk", SysUser::getUsername, LabMaininfo::getChkDoctor);
        mpjLambdaWrapper.ge(BeanUtil.isNotEmpty(labMaininfoPageParam.getStartTime()), LabMaininfo::getSampleda, labMaininfoPageParam.getStartTime());
        mpjLambdaWrapper.le(BeanUtil.isNotEmpty(labMaininfoPageParam.getEndTime()), LabMaininfo::getSampleda, labMaininfoPageParam.getEndTime());
        mpjLambdaWrapper.eq(StrUtil.isNotEmpty(labMaininfoPageParam.getPatNo()), LabMaininfo::getPatNo, labMaininfoPageParam.getPatNo());
        mpjLambdaWrapper.eq(LabMaininfo::getIsChk, 2)
                .eq(StrUtil.isNotEmpty(labMaininfoPageParam.getRptGroup()), LabMaininfo::getRptGroup, labMaininfoPageParam.getRptGroup())
                .eq(LabMaininfo::getDelFlag, 0)
                .orderByDesc(LabMaininfo::getSampleda);
        List<LabMaininfo> list = labMaininfoMapper.selectList(mpjLambdaWrapper);
        return Result.succ(1, "查询成功", list);
    }

    @PostMapping("/auditRe")
    public Result auditRe(@RequestBody JSONArray jsonArray) {
        System.out.println(jsonArray);
        SysUser sysUser = RedisUserManager.getUser();

        LambdaQueryWrapper<SysInterface> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysInterface::getDelFlag, 0);
        wrapper.eq(SysInterface::getIsEnable, 1);
        wrapper.eq(SysInterface::getOrgId, sysUser.getOrgId());
        List<SysInterface> sysInterfaces = sysInterfaceMapper.selectList(wrapper);
        List<String> collect = sysInterfaces.stream().filter(o -> Objects.nonNull(o) && StringUtils.isNotBlank(o.getInterfaceUrl())).map(SysInterface::getInterfaceUrl).distinct().collect(Collectors.toList());
        if (com.baomidou.mybatisplus.core.toolkit.CollectionUtils.isNotEmpty(collect)) {
            /*发布报告相关*/
            for (String webUrl : collect) {
                String releaseReport = webUrl + "/zUpdateProblermData/updateProblermData";
                HttpUtil.createPost(releaseReport).setConnectionTimeout(3000).body(JSONUtil.toJsonStr(jsonArray)).contentType("application/json").execute().body();
                for (Object o : jsonArray) {
                    LabMaininfo labMaininfo = new LabMaininfo();
                    labMaininfo.setSampleda(labMaininfoMapper.selectById(o.toString()).getSampleda());
                    labMaininfo.setRemark1("已重审");
                    labMaininfo.setRemark2(DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss"));
                    labMaininfo.setId(o.toString());
                    labMaininfoMapper.updateById(labMaininfo);
                }
            }
        }
        return Result.succ(1,"重审成功","");
    }


    @GetMapping("/findInterfaceList")
    public Result findInterfaceList(String systemId, String moduleId) {
        LambdaQueryWrapper<SysInterface> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SysInterface::getSystemId, systemId)
                .eq(SysInterface::getModuleId, moduleId)
                .eq(SysInterface::getDelFlag, 0);
        List<SysInterface> list = sysInterfaceService.list(lambdaQueryWrapper);
        return Result.succ(1, "查询成功", list);
    }
}
